package com.penghaisoft.microwms.materialhand.web.outwarehouse.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.data.model.RawHandOut;
import com.penghaisoft.microwms.common.data.model.RawHandOutSn;
import com.penghaisoft.microwms.common.data.model.RawRequisition;
import com.penghaisoft.microwms.common.data.model.RawRequisitionSn;
import com.penghaisoft.microwms.common.dto.RawBackDto;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.service.BaseService;
import com.penghaisoft.microwms.common.wms.model.RawBack;
import com.penghaisoft.microwms.common.wms.model.RawStorageSn;
import com.penghaisoft.microwms.materialhand.web.backmat.dao.RawBackMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawHandOutMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawHandOutSnMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawRequisitionMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.dao.RawRequisitionSnMapper;
import com.penghaisoft.microwms.materialhand.web.outwarehouse.service.IMaterialHandOutWhService;
import com.penghaisoft.microwms.materialhand.web.storage.dao.RawStorageSnMapper;

/**
 * @program: micro-wms
 * @description: 原材料手持出库服务实现
 * @author: zhangx
 * @create: 2018-05-17 15:30
 **/
@Service
public class MaterialHandOutWhServiceImpl extends BaseService implements IMaterialHandOutWhService {

    @Autowired
    private RawBackMapper rawBackMapper;

    @Autowired
    private RawStorageSnMapper rawStorageSnMapper;

    @Autowired
    private RawHandOutMapper rawHandOutMapper;
    
    @Autowired
    private RawHandOutSnMapper rawHandOutSnMapper;
    
    @Autowired
    private RawRequisitionSnMapper rawRequisitionSnMapper;
    
    @Autowired
    private RawRequisitionMapper rawRequisitionMapper;
    
    /** 
     * @Title: backMatScanBarCheck 
     * @Description: 退料校验
     * @param @param rawBackDto
     * @param @return  参数说明 
     * @return TransResult    返回类型
     * @author luoteng 
     * @date 2018年10月15日 下午1:08:52 
     * @throws 
     */
    @Transactional
    @Override
    public TransResult backMatScanBarCheck(RawBackDto rawBackDto) {
        TransResult resp = new TransResult();
        resp.setCode(Constant.SUCCESS);
        String barcode = rawBackDto.getBarcode();
        
        RawRequisitionSn updateRawRequisitionSn = new RawRequisitionSn();
        
        RawRequisitionSn rawRequisitionSn = new RawRequisitionSn();
        rawRequisitionSn.setBarcode(barcode);
        List<RawRequisitionSn> rawRequisitionSnList = rawRequisitionSnMapper.queryStatusByBarcode(rawRequisitionSn);
        if(rawRequisitionSnList == null || rawRequisitionSnList.isEmpty()){
       	 	resp.setCode(Constant.FAIL);
            resp.setMsg("该条码非领料出库的条码，不能进行退料！");
            return resp;
        }else{
        	updateRawRequisitionSn = rawRequisitionSnList.get(0);
////        	领料单状态 0：新建 1：发货中 2：发货完成
//        	if(!updateRawRequisitionSn.getRequisitionStatus().equals("2")){
//        		resp.setCode(Constant.FAIL);
//                resp.setMsg("该条码对应领料单正在出库，不能进行退料！");
//                return resp;
//        	}
        	
//        	MES回传标识 0：未传给MES;1：已传MES
        	if(updateRawRequisitionSn.getMesStatus() != null && !updateRawRequisitionSn.getMesStatus().equals("1")) {
        		resp.setMsg("该条码未传给MES，不能进行退料！");
              return resp;
        	}
        }

//        1 查询条码是否存在且为出库
        RawStorageSn storageSn = new RawStorageSn();
        storageSn.setBarcode(barcode);
        //  0可用；1 冻结；2已出库 3 报废
//        storageSn.setBarcodeStatus("2");
        storageSn.setActiveFlag("1");
        List<RawStorageSn> snList = rawStorageSnMapper.queryByAny(storageSn);
        if(snList == null || snList.isEmpty()){
            resp.setCode(Constant.FAIL);
            resp.setMsg("该条码不存在！");
            return resp;
        }else if(snList.size() > 1){
        	resp.setCode(Constant.FAIL);
            resp.setMsg("该条码库存记录大于1条，请联系运维人员！");
            return resp;
        }else{
//        2 修改条码明细为可用
            RawStorageSn rawStorageSn = snList.get(0);
            
            String barcodeStatus = rawStorageSn.getBarcodeStatus();
            if("0".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码未出库或者已经退回，不能进行退料操作！");
                return resp;
            }else if("1".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码已冻结，不能进行退料操作！");
                return resp;
            }else if("3".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码已报废，不能进行退料操作！");
                return resp;
            }
            
            BigDecimal amount = rawStorageSn.getAmount();
            
            RawHandOutSn rawHandOutSn = new RawHandOutSn();
            rawHandOutSn.setBarcode(barcode);
            List<RawHandOutSn> rawHandOutSnList = rawHandOutSnMapper.queryStatusByBarcode(rawHandOutSn);
            if(rawHandOutSnList != null && !rawHandOutSnList.isEmpty()){
            	 resp.setCode(Constant.FAIL);
                 resp.setMsg("该条码正在手工出库，不能进行退料！");
                 return resp;
            }
            
//          型号类型 	LW:螺纹钢管 PE:PE管WF:无缝钢管 BL:白料 HL:黑料 WT:弯头 ST:三通 BJ:变径GD:固定支架BC:补偿器
            if(rawStorageSn.getModelType() != null && (rawStorageSn.getModelType().equals(Constant.MODEL_PE) || rawStorageSn.getModelType().equals(Constant.MODEL_W)
            		|| rawStorageSn.getModelType().equals(Constant.MODEL_L))){
            	if(rawBackDto.getAmount() == null){
            		resp.setCode(Constant.FAIL);
                    resp.setMsg("退PE管、无缝钢管、螺旋钢管必须输入长度！");
                    return resp;
            	}
            	
            	String b = "[0-9]{1,}(\\.[0-9]{1,})?";
                Pattern pattern = Pattern.compile(b);
                Matcher matcher = pattern.matcher(rawBackDto.getAmount().toString());
                Boolean isNt = matcher.matches();
                if (isNt){
                	amount = rawBackDto.getAmount();
                	rawStorageSn.setAmount(rawBackDto.getAmount());
                }else {
                	resp.setCode(Constant.FAIL);
                	resp.setMsg("长度非数字格式！");
                    return resp;
                }
            }
            
            updateRawRequisitionSn.setBackAmount(amount);
            
            resp.setData(updateRawRequisitionSn);
        }

        return resp;
    }
    
    /** 
     * @Title: backMatScanBar 
     * @Description: 退料
     * @param @param rawBackDto
     * @param @return  参数说明 
     * @return TransResult    返回类型
     * @author luoteng 
     * @date 2018年10月15日 下午1:08:52 
     * @throws 
     */
    @Transactional
    @Override
    public TransResult backMatScanBar(RawBackDto rawBackDto) {
        TransResult resp = new TransResult();
        resp.setCode(Constant.SUCCESS);
        String barcode = rawBackDto.getBarcode();
        Date now = new Date();
        String loginName = rawBackDto.getLoginName();
        
        RawRequisitionSn updateRawRequisitionSn = new RawRequisitionSn();
        
        RawRequisitionSn rawRequisitionSn = new RawRequisitionSn();
        rawRequisitionSn.setBarcode(barcode);
        List<RawRequisitionSn> rawRequisitionSnList = rawRequisitionSnMapper.queryStatusByBarcode(rawRequisitionSn);
        if(rawRequisitionSnList == null || rawRequisitionSnList.isEmpty()){
       	 	resp.setCode(Constant.FAIL);
            resp.setMsg("该条码非领料出库的条码，不能进行退料！");
            return resp;
        }else{
        	updateRawRequisitionSn = rawRequisitionSnList.get(0);
////        	领料单状态 0：新建 1：发货中 2：发货完成
//        	if(!updateRawRequisitionSn.getRequisitionStatus().equals("2")){
//        		resp.setCode(Constant.FAIL);
//                resp.setMsg("该条码对应领料单正在出库，不能进行退料！");
//                return resp;
//        	}
        	
//        	MES回传标识 0：未传给MES;1：已传MES
        	if(updateRawRequisitionSn.getMesStatus() != null && !updateRawRequisitionSn.getMesStatus().equals("1")) {
        		resp.setMsg("该条码未传给MES，不能进行退料！");
              return resp;
        	}
        }

//        1 查询条码是否存在且为出库
        RawStorageSn storageSn = new RawStorageSn();
        storageSn.setBarcode(barcode);
        //  0可用；1 冻结；2已出库 3 报废
//        storageSn.setBarcodeStatus("2");
        storageSn.setActiveFlag("1");
        List<RawStorageSn> snList = rawStorageSnMapper.queryByAny(storageSn);
        if(snList == null || snList.isEmpty()){
            resp.setCode(Constant.FAIL);
            resp.setMsg("该条码不存在！");
            return resp;
        }else if(snList.size() > 1){
        	resp.setCode(Constant.FAIL);
            resp.setMsg("该条码库存记录大于1条，请联系运维人员！");
            return resp;
        }else{
//        2 修改条码明细为可用
            RawStorageSn rawStorageSn = snList.get(0);
            
            String barcodeStatus = rawStorageSn.getBarcodeStatus();
            if("0".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码未出库或者已经退回，不能进行退料操作！");
                return resp;
            }else if("1".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码已冻结，不能进行退料操作！");
                return resp;
            }else if("3".equals(barcodeStatus)){
            	resp.setCode(Constant.FAIL);
                resp.setMsg("该条码已报废，不能进行退料操作！");
                return resp;
            }
            
            RawHandOutSn rawHandOutSn = new RawHandOutSn();
            rawHandOutSn.setBarcode(barcode);
            List<RawHandOutSn> rawHandOutSnList = rawHandOutSnMapper.queryStatusByBarcode(rawHandOutSn);
            if(rawHandOutSnList != null && !rawHandOutSnList.isEmpty()){
            	 resp.setCode(Constant.FAIL);
                 resp.setMsg("该条码正在手工出库，不能进行退料！");
                 return resp;
            }
            
            String model = rawStorageSn.getModel();
            String modelType = rawStorageSn.getModelType();
            String contractNo = rawStorageSn.getContractNo();
            String purchaseNo = rawStorageSn.getPurchaseNo();
            BigDecimal amount = rawStorageSn.getAmount();
            BigDecimal oldAmount = rawStorageSn.getAmount();
            String unit = rawStorageSn.getUnit();
            String wlCode = rawBackDto.getWlCode();
            
//          型号类型 	LW:螺纹钢管 PE:PE管WF:无缝钢管 BL:白料 HL:黑料 WT:弯头 ST:三通 BJ:变径GD:固定支架BC:补偿器
            if(rawStorageSn.getModelType() != null && (rawStorageSn.getModelType().equals(Constant.MODEL_PE) || rawStorageSn.getModelType().equals(Constant.MODEL_W)
            		|| rawStorageSn.getModelType().equals(Constant.MODEL_L))){
            	if(rawBackDto.getAmount() == null){
            		resp.setCode(Constant.FAIL);
                    resp.setMsg("退PE管、无缝钢管、螺旋钢管必须输入长度！");
                    return resp;
            	}
            	
            	String b = "[0-9]{1,}(\\.[0-9]{1,})?";
                Pattern pattern = Pattern.compile(b);
                Matcher matcher = pattern.matcher(rawBackDto.getAmount().toString());
                Boolean isNt = matcher.matches();
                if (isNt){
                	rawStorageSn.setAmount(rawBackDto.getAmount());
                }else {
                	resp.setCode(Constant.FAIL);
                	resp.setMsg("长度非数字格式！");
                    return resp;
                }
                
                amount = rawBackDto.getAmount();
                
                if(amount.compareTo(oldAmount) > 0) {
                	resp.setCode(Constant.FAIL);
                	resp.setMsg("当前条码退料长度大于原长度！");
                    return resp;
                }
                
//            	rawStorageSn.setStorageSnId(null);
            //  0可用；1 冻结；2已出库 3 报废
            	rawStorageSn.setBarcodeStatus("0");
//            	rawStorageSn.setGmtCreate(now);
//            	rawStorageSn.setCreateBy(loginName);
            	rawStorageSn.setGmtModified(now);
            	rawStorageSn.setLastModifiedBy(loginName);
            	rawStorageSn.setWlCode(wlCode);
            	
//            	rawStorageSnMapper.create(rawStorageSn);
				rawStorageSnMapper.updateBySelect(rawStorageSn);
            }else{
            	int snId = rawStorageSn.getStorageSnId();
                
                RawStorageSn updSn = new RawStorageSn();
                updSn.setStorageSnId(snId);
                updSn.setWlCode(wlCode);
                //  0可用；1 冻结；2已出库 3 报废
                updSn.setBarcodeStatus("0");
                updSn.setGmtModified(now);
                updSn.setLastModifiedBy(loginName);
                rawStorageSnMapper.updateBySelect(updSn);
            }
            
//        3 写入退料表
            RawBack rawBack = new RawBack();
			rawBack.setOldAmount(oldAmount);
            rawBack.setAmount(amount);
            rawBack.setBarcode(barcode);
            //  0 正常 1 退扫
            rawBack.setBackStatus("0");
            rawBack.setContractNo(contractNo);
            rawBack.setPurchaseNo(purchaseNo);
            rawBack.setModel(model);
            rawBack.setModelType(modelType);
            rawBack.setWlCode(wlCode);
            rawBack.setUnit(unit);
            rawBack.setGmtCreate(now);
            rawBack.setCreateBy(loginName);
            rawBack.setActiveFlag("1");
            rawBack.setUserDefined1(updateRawRequisitionSn.getRequisitionNo());
//            领料单类型
            rawBack.setUserDefined2(updateRawRequisitionSn.getRequisitionType());
            rawBackMapper.create(rawBack);
            
            updateRawRequisitionSn.setBackAmount(amount);
            rawRequisitionSnMapper.updateBySelect(updateRawRequisitionSn);
            
            RawRequisition rawRequisition = new RawRequisition();
            rawRequisition.setRequisitionId(updateRawRequisitionSn.getRequisitionId());
            rawRequisition.setBackAmountAdd(amount);
            rawRequisitionMapper.updateBySelect(rawRequisition);

            resp.setData(rawStorageSn);
            resp.setMsg(wlCode);
        }

        return resp;
    }


	/* @Title: queryHandworkOut 
	 * @Description: 手工出库单号
	 * @author jzh
	 * @date 2018年6月4日 下午1:23:44 
	 */
	@Override
	public List<RawHandOut> queryHandworkOut(RawHandOut rawHandOut) {
	
		return rawHandOutMapper.queryByAny(rawHandOut);
	}


	/* @Title: updateHandworkOut 
	 * @Description: 将手工出库单状态置为发货中
	 * @author jzh
	 * @date 2018年6月4日 下午1:25:35 
	 */
	@Override
	public Resp updateHandworkOut(RawHandOut rawHandOut) {
		
		rawHandOut.setHandOutStatus("1");
		
		Integer updateCnt = rawHandOutMapper.update(rawHandOut);
		if(updateCnt.intValue() == 1){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}


	@Override
	public List<RawHandOut> queryHandworkOutNot2(RawHandOut rawHandOut) {
		
		return rawHandOutMapper.queryHandworkNot2(rawHandOut);
	}


	/* @Title: queryRawStorageSn 
	 * @Description: 根据条码查询，状态为可用
	 * @author jzh
	 * @date 2018年6月5日 上午9:40:48 
	 */
	@Override
	public List<RawStorageSn> queryRawStorageSn(RawStorageSn rawStorageSn) {
		
		return rawStorageSnMapper.queryByAny(rawStorageSn);
	}


	/* @Title: updateAndCreateHandOut 
	 * @Description: 更改已分配数量为新数量 并且 新增记录,更改条码状态为已出库（保证事物）
	 * @author jzh
	 * @date 2018年6月5日 下午1:42:20 
	 */
	@Override
	@Transactional//事物
	public Resp updateAndCreateHandOut(RawHandOut rawHandOutNew, RawHandOutSn rawHandOutSn, RawStorageSn rawStorageSnNew) {
		//入参：rawHandOutNew(更改新数量)，rawHandOut2，barcode（记录所需实体，条码）
		//修改手工出库单新实际数量数量
		Integer updateCnt = rawHandOutMapper.updateNewRealAmount(rawHandOutNew);
		if (updateCnt.intValue() <= 0) {
			throw new RuntimeException("更改数量失败");
		}
		//新增记录RawHandOutSn实体
		Integer createCnt = rawHandOutSnMapper.create(rawHandOutSn);
		if (createCnt.intValue() <= 0) {
			throw new RuntimeException("新增记录失败");
		}
		//更改条码状态为已出库
		Integer updateStorageSnCnt = rawStorageSnMapper.updateBySelect(rawStorageSnNew);
		if (updateStorageSnCnt.intValue() <= 0) {
			throw new RuntimeException("更改条码状态为已出库失败");
		}

		return success();
	}


	/* @Title: handworkBack 
	 * @Description: 
	 * //将RawHandOutSn的scan_status置为退扫，将rawHandOut实发数量-数量，
	   //将rawStorageSn的BarcodeStatus置为0可用（保证事物）
	 * @author jzh
	 * @date 2018年6月6日 下午3:25:22 
	 */
	@Override
	@Transactional//事物
	public Resp handworkBack(RawHandOutSn rawHandOutSn, RawHandOut rawHandOut, RawStorageSn rawStorageSn) {
		//将RawHandOutSn的scan_status置为退扫
		Integer updateCnt = rawHandOutSnMapper.updateHandOutSn(rawHandOutSn);
		if (updateCnt.intValue() <= 0) {
			throw new RuntimeException("退扫失败");
		}else if(updateCnt.intValue() > 1){
			throw new RuntimeException("退扫失败,更新数量不唯一");
		}
		//将rawHandOut实发数量-数量
		Integer updateHandOut = rawHandOutMapper.updateBySelect(rawHandOut);
		if (updateHandOut.intValue() <= 0) {
			throw new RuntimeException("更改数量失败");
		}
		//将rawStorageSn的BarcodeStatus置为0可用
		Integer updateStorageSn = rawStorageSnMapper.updateStorageSn(rawStorageSn);
		if (updateStorageSn.intValue() <= 0) {
			throw new RuntimeException("退扫失败");
		}else if(updateStorageSn.intValue() > 1){
			throw new RuntimeException("退扫失败,更新库存数量不唯一");
		}
		
		return success();
	}


	/* @Title: updateHandwork 
	 * @Description: 将出库单置为已发货2
	 * @author jzh
	 * @date 2018年6月7日 上午11:34:58 
	 */
	@Override
	public Resp updateHandwork(RawHandOut rawHandOut) {
		Integer updateCnt = rawHandOutMapper.updateByhandOutNo(rawHandOut);
		System.out.print("更改数量："+updateCnt);
		if(updateCnt.intValue() >0){
			return success();
		}else{
			return fail("更改状态失败");
		}
	}
	
	/**  
	 * @Title: isOrNotHandOut
	 * @Description:   判断是否为手工出库
	 * @author Lengxf  
	 * @date 2019年3月4日    
	 */  
	public TransResult isOrNotHandOut(RawBackDto rawBackDto){
		TransResult result = new TransResult();
		result.setCode(Constant.SUCCESS);
		String barcode = rawBackDto.getBarcode();
		boolean isHandOut = false;//手工出库
		boolean isRequisitionOut = false;//领料出库
		
		RawHandOutSn rawHandOutSn = new RawHandOutSn();
		rawHandOutSn.setBarcode(barcode);
		List<RawHandOutSn> rawHandOutSnLis = rawHandOutSnMapper.queryHandOutSnByDesc(rawHandOutSn);
		
		RawRequisitionSn rawRequisitionSn = new RawRequisitionSn();
		rawRequisitionSn.setBarcode(barcode);
		List<RawRequisitionSn> rawRequisitionSnList = rawRequisitionSnMapper.queryRawRequisitionSnBydesc(rawRequisitionSn);
		
		if(rawHandOutSnLis != null && !rawHandOutSnLis.isEmpty()){
			isHandOut = true;
		}
		
		if(rawRequisitionSnList != null && !rawRequisitionSnList.isEmpty()){
			isRequisitionOut = true;
		}
		//判断最后的出库方式，Y代表手工出库，N代表领料出库
		if(isHandOut && isRequisitionOut){
			RawRequisitionSn reqsn = rawRequisitionSnList.get(0);
			RawHandOutSn handsn = rawHandOutSnLis.get(0);
			Integer num = reqsn.getGmtCreate().compareTo(handsn.getGmtCreate());
			if(num > 0){
				result.setMsg("N");
			}else{
				result.setMsg("Y");
			}
		}else if(isHandOut && !isRequisitionOut){
			result.setMsg("Y");
		}else if(!isHandOut && isRequisitionOut){
			result.setMsg("N");
		}else{
			result.setMsg("该条码不存在！");
			result.setCode(Constant.FAIL);
			return result;
		}
		
		return result;
	}
	
	/**  
	 * @Title: backHandOutCheck
	 * @Description:   手工出库退料校验+退料
	 * @author Lengxf  
	 * @date 2019年3月4日    
	 */ 
	 @Transactional
	 @Override
	public TransResult backHandOutCheck(RawBackDto rawBackDto){
		TransResult resp = new TransResult();
		resp.setCode(Constant.SUCCESS);
		String loginName = rawBackDto.getLoginName();
		Date now = new Date();
		String barcode = rawBackDto.getBarcode();
		RawHandOutSn rawHandOutSnentity = new RawHandOutSn();
		rawHandOutSnentity.setBarcode(barcode);
		List<RawHandOutSn> rawHandOutSnLis = rawHandOutSnMapper.queryHandOutSnByDesc(rawHandOutSnentity);
		if(rawHandOutSnLis == null ||rawHandOutSnLis.isEmpty()){
			resp.setCode(Constant.FAIL);
			resp.setMsg("参数错误");
			return resp;
		}else{
			RawHandOutSn handSn = rawHandOutSnLis.get(0);
			if(handSn.getBackAmount() != null && "".equals(handSn.getBackAmount())){
				resp.setCode(Constant.FAIL);
				resp.setMsg("该手工出库单已完成退料操作，请勿重复操作");
				return resp;
				}else{

//			        1 查询条码是否存在且为出库
			        RawStorageSn storageSn = new RawStorageSn();
			        storageSn.setBarcode(barcode);
			        //  0可用；1 冻结；2已出库 3 报废
//			        storageSn.setBarcodeStatus("2");
			        storageSn.setActiveFlag("1");
			        List<RawStorageSn> snList = rawStorageSnMapper.queryByAny(storageSn);
			        if(snList == null || snList.isEmpty()){
			            resp.setCode(Constant.FAIL);
			            resp.setMsg("该条码不存在！");
			            return resp;
			        }else if(snList.size() > 1){
			        	resp.setCode(Constant.FAIL);
			            resp.setMsg("该条码库存记录大于1条，请联系运维人员！");
			            return resp;
			        }else{
//			        2 修改条码明细为可用
			            RawStorageSn rawStorageSn = snList.get(0);
			            
			            String barcodeStatus = rawStorageSn.getBarcodeStatus();
			            if("0".equals(barcodeStatus)){
			            	resp.setCode(Constant.FAIL);
			                resp.setMsg("该条码未出库或者已经退回，不能进行退料操作！");
			                return resp;
			            }else if("1".equals(barcodeStatus)){
			            	resp.setCode(Constant.FAIL);
			                resp.setMsg("该条码已冻结，不能进行退料操作！");
			                return resp;
			            }else if("3".equals(barcodeStatus)){
			            	resp.setCode(Constant.FAIL);
			                resp.setMsg("该条码已报废，不能进行退料操作！");
			                return resp;
			            }
			            
			            
			            String model = rawStorageSn.getModel();
			            String modelType = rawStorageSn.getModelType();
			            String contractNo = rawStorageSn.getContractNo();
			            String purchaseNo = rawStorageSn.getPurchaseNo();
			            BigDecimal amount = rawStorageSn.getAmount();
			            BigDecimal oldAmount = rawStorageSn.getAmount();
			            String unit = rawStorageSn.getUnit();
			            String wlCode = rawBackDto.getWlCode();
			            
//			          型号类型 	LW:螺纹钢管 PE:PE管WF:无缝钢管 BL:白料 HL:黑料 WT:弯头 ST:三通 BJ:变径GD:固定支架BC:补偿器
			            if(rawStorageSn.getModelType() != null 
			            		&& (rawStorageSn.getModelType().equals(Constant.MODEL_PE) 
			            				|| rawStorageSn.getModelType().equals(Constant.MODEL_W)
			            					|| rawStorageSn.getModelType().equals(Constant.MODEL_L))){
			            	if(rawBackDto.getAmount() == null){
			            		resp.setCode(Constant.FAIL);
			                    resp.setMsg("退PE管、无缝钢管、螺旋钢管必须输入长度！");
			                    return resp;
			            	}
			            	
			            	String b = "[0-9]{1,}(\\.[0-9]{1,})?";
			                Pattern pattern = Pattern.compile(b);
			                Matcher matcher = pattern.matcher(rawBackDto.getAmount().toString());
			                Boolean isNt = matcher.matches();
			                if (isNt){
			                	rawStorageSn.setAmount(rawBackDto.getAmount());
			                }else {
			                	resp.setCode(Constant.FAIL);
			                	resp.setMsg("长度非数字格式！");
			                    return resp;
			                }
			                
			                amount = rawBackDto.getAmount();
			                
			                if(amount.compareTo(oldAmount) > 0) {
			                	resp.setCode(Constant.FAIL);
			                	resp.setMsg("当前条码退料长度大于原长度！");
			                    return resp;
			                }
			                
//			            	rawStorageSn.setStorageSnId(null);
			            //  0可用；1 冻结；2已出库 3 报废
			            	rawStorageSn.setBarcodeStatus("0");
//			            	rawStorageSn.setGmtCreate(now);
//			            	rawStorageSn.setCreateBy(loginName);
			            	rawStorageSn.setGmtModified(now);
			            	rawStorageSn.setLastModifiedBy(loginName);
			            	rawStorageSn.setWlCode(wlCode);
			            	
//			            	rawStorageSnMapper.create(rawStorageSn);
							rawStorageSnMapper.updateBySelect(rawStorageSn);
			            }else{
			            	int snId = rawStorageSn.getStorageSnId();
			                
			                RawStorageSn updSn = new RawStorageSn();
			                updSn.setStorageSnId(snId);
			                updSn.setWlCode(wlCode);
			                //  0可用；1 冻结；2已出库 3 报废
			                updSn.setBarcodeStatus("0");
			                updSn.setGmtModified(now);
			                updSn.setLastModifiedBy(loginName);
			                rawStorageSnMapper.updateBySelect(updSn);
			            }
			            
//			        3 写入退料表
			            RawBack rawBack = new RawBack();
						rawBack.setOldAmount(oldAmount);
			            rawBack.setAmount(amount);
			            rawBack.setBarcode(barcode);
			            //  0 正常 1 退扫
			            rawBack.setBackStatus("0");
			            rawBack.setContractNo(contractNo);
			            rawBack.setPurchaseNo(purchaseNo);
			            rawBack.setModel(model);
			            rawBack.setModelType(modelType);
			            rawBack.setWlCode(wlCode);
			            rawBack.setUnit(unit);
			            rawBack.setGmtCreate(now);
			            rawBack.setCreateBy(loginName);
			            rawBack.setActiveFlag("1");
			            rawBack.setUserDefined1("手工出库退料");
//			            领料单类型
			            rawBack.setUserDefined2("手工出库退料");
			            rawBackMapper.create(rawBack);
			            
			            handSn.setBackAmount(amount);
			            handSn.setLastModifiedBy(loginName);
			            handSn.setGmtModified(now);
			            rawHandOutSnMapper.updateBySelect(handSn);
			            
			            RawHandOut rawHandOut = new RawHandOut();
			            rawHandOut.setHandOutNo(handSn.getHandOutNo());
			            rawHandOut.setBackAmount(amount);
			            rawHandOut.setModel(handSn.getModel());
			            rawHandOut.setLastModifiedBy(loginName);
			            rawHandOut.setGmtModified(now);
			            rawHandOutMapper.updateBackAmountByModel(rawHandOut);
			            
			            resp.setData(rawStorageSn);
			            resp.setMsg(wlCode);
			            return resp;
			        }
				}
			}
		
	}


}
